package pl.streamsoft.test.currency_view.storage;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.swing.table.AbstractTableModel;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import pl.streamsoft.test.currency_buisness.CurrenciesService;
import pl.streamsoft.test.currency_buisness.dto.CurrencyDTO;
import pl.streamsoft.test.currency_buisness.exceptions.ServiceException;

@Component
@Scope(value = "singleton")
public class CurrencyTableModel extends AbstractTableModel {

	private Date lastFromDate;
	private Date lastToDate;
	private String lastCurrencyAcronym;

	@Autowired
	private CurrenciesService service;

	@Autowired
	private Storage storage;

	private static final long serialVersionUID = 1344857657286134109L;
	static final String[] COLUMN_NAMES = { "Data", "Nazwa", "Akronim",
			"Wartość" };
	private ArrayList<CurrencyDTO> data;

	public CurrencyTableModel() {
		super();
		data = new ArrayList<CurrencyDTO>();
	}

	public Class<?> getColumnClass(int columnIndex) {
		return String.class;
	}

	public int getColumnCount() {
		return COLUMN_NAMES.length;
	}

	public String getColumnName(int columnIndex) {
		return columnIndex <= COLUMN_NAMES.length ? COLUMN_NAMES[columnIndex]
				: "";
	}

	public int getRowCount() {
		return data.size();
	}

	public Object getValueAt(int rowIndex, int columnIndex) {

		switch (columnIndex) {
		case 0:
			SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
			return formatter.format(data.get(rowIndex).date);
		case 1:
			return data.get(rowIndex).name;
		case 2:
			return data.get(rowIndex).acronym;
		case 3:
			return data.get(rowIndex).value;
		}
		return "";
	}

	public CurrencyDTO getValueAt(int rowIndex) {
		return data.size() > rowIndex ? data.get(rowIndex) : null;
	}

	public boolean isCellEditable(int rowIndex, int columnIndex) {
		return false;
	}

	public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
	}

	public void clear() {
		data.clear();
	}

	public void replaceElementsWith(List<CurrencyDTO> currenciesList) {
		clear();
		if (currenciesList != null) {
			data.addAll(currenciesList);
		}
		fireTableRowsInserted(0, currenciesList.size());
	}

	public void updateFromService(Date from, Date to, String currencyAcronym)
			throws ServiceException {
		List<CurrencyDTO> currencyTypesList = service.getCurrencies(from, to,
				currencyAcronym);

		replaceElementsWith(currencyTypesList);
		lastFromDate = from;
		lastToDate = to;
		lastCurrencyAcronym = currencyAcronym;
		storage.averageUpdateSignal.execute();
	}

	public void reloadFromServer() throws ServiceException {
		Date fromDate = lastFromDate != null ? lastFromDate : new Date();
		Date toDate = lastToDate != null ? lastToDate : new Date();

		updateFromService(fromDate, toDate, lastCurrencyAcronym);

		lastFromDate = fromDate;
		lastToDate = toDate;
	}

}
